package com.java.al.swing;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;
import java.io.*;


public class DrawLine
{
	public static void main(String args[])
	{
		new paintboard("鐢诲浘绋嬪簭");
	}
}
class Point implements Serializable
{
	int x, y;
	Color col;
	int tool;
	int boarder;

	Point(int x, int y, Color col, int tool, int boarder)
	{
		this.x = x;
		this.y = y;
		this.col = col;
		this.tool = tool;
		this.boarder = boarder;
	}
}

class paintboard extends JFrame implements ActionListener, MouseMotionListener,
		MouseListener, ItemListener
{
	int x = -1, y = -1;
	int con = 1;// 鐢荤瑪澶у皬
	int Econ = 5;// 姗＄毊澶у皬

	int toolFlag = 0;// toolFlag:宸ュ叿鏍囪
	// toolFlag宸ュ叿瀵瑰簲琛細
	// 锛�--鐢荤瑪锛夛紱锛�--姗＄毊锛夛紱锛�--娓呴櫎锛夛紱
	// 锛�--鐩寸嚎锛夛紱锛�--鍦嗭級锛涳紙5--鐭╁舰锛夛紱

	Color c = new Color(0, 0, 0); // 鐢荤瑪棰滆壊
	BasicStroke size = new BasicStroke(con, BasicStroke.CAP_BUTT,
			BasicStroke.JOIN_BEVEL);// 鐢荤瑪绮楃粏
	Point cutflag = new Point(-1, -1, c, 6, con);// 鎴柇鏍囧織

	Vector paintInfo = null;// 鐐逛俊鎭悜閲忕粍
	int n = 1;

	FileInputStream picIn = null;
	FileOutputStream picOut = null;

	ObjectInputStream VIn = null;
	ObjectOutputStream VOut = null;

	// *宸ュ叿闈㈡澘--鐢荤瑪锛岀洿绾匡紝鍦嗭紝鐭╁舰锛屽杈瑰舰,姗＄毊锛屾竻闄�/
	JPanel toolPanel;
	Button eraser, drLine, drCircle, drRect;
	Button clear, pen;
	Choice ColChoice, SizeChoice, EraserChoice;
	Button colchooser;
	Label 棰滆壊, 澶у皬B, 澶у皬E;
	// 淇濆瓨鍔熻兘
	Button openPic, savePic;
	FileDialog openPicture, savePicture;

	paintboard(String s)
	{
		super(s);
		addMouseMotionListener(this);
		addMouseListener(this);

		paintInfo = new Vector();

		/* 鍚勫伐鍏锋寜閽強閫夋嫨椤�*/
		// 棰滆壊閫夋嫨
		ColChoice = new Choice(); //棰滆壊閫夋嫨涓嬫媺鍒楄〃妗�
		ColChoice.add("black");
		ColChoice.add("red");
		ColChoice.add("blue");
		ColChoice.add("green");
		ColChoice.addItemListener(this);

		// 鐢荤瑪澶у皬閫夋嫨
		SizeChoice = new Choice(); //鐢荤瑪澶у皬涓嬫媺鍒楄〃妗�
		SizeChoice.add("1");
		SizeChoice.add("3");
		SizeChoice.add("5");
		SizeChoice.add("7");
		SizeChoice.add("9");
		SizeChoice.addItemListener(this);

		// 姗＄毊澶у皬涓嬫媺鍒楄〃妗�
		EraserChoice = new Choice();
		EraserChoice.add("5");
		EraserChoice.add("9");
		EraserChoice.add("13");
		EraserChoice.add("17");
		EraserChoice.addItemListener(this);
		// //////////////////////////////////////////////////
		toolPanel = new JPanel();

		clear = new Button("娓呴櫎");
		eraser = new Button("姗＄毊");
		pen = new Button("鐢荤瑪");
//		drLine = new Button("鐢荤洿绾�);
//		drCircle = new Button("鐢诲渾褰�);
//		drRect = new Button("鐢荤煩褰�);

		openPic = new Button("鎵撳紑鍥剧敾");
		savePic = new Button("淇濆瓨鍥剧敾");

//		colchooser = new Button("鏄剧ず璋冭壊鏉�);

		// 鍚勭粍浠朵簨浠剁洃鍚�
		clear.addActionListener(this);
		eraser.addActionListener(this);
		pen.addActionListener(this);
		drLine.addActionListener(this);
		drCircle.addActionListener(this);
		drRect.addActionListener(this);
		openPic.addActionListener(this);
		savePic.addActionListener(this);
		colchooser.addActionListener(this);

		棰滆壊 = new Label("鐢荤瑪棰滆壊", Label.CENTER);
		澶у皬B = new Label("鐢荤瑪澶у皬", Label.CENTER);
		澶у皬E = new Label("姗＄毊澶у皬", Label.CENTER);
		// 闈㈡澘娣诲姞缁勪欢
		toolPanel.add(openPic);
		toolPanel.add(savePic);

		toolPanel.add(pen);
		toolPanel.add(drLine);
		toolPanel.add(drCircle);
		toolPanel.add(drRect);

		toolPanel.add(棰滆壊);
		toolPanel.add(ColChoice);
		toolPanel.add(澶у皬B);
		toolPanel.add(SizeChoice);
		toolPanel.add(colchooser);

		toolPanel.add(eraser);
		toolPanel.add(澶у皬E);
		toolPanel.add(EraserChoice);

		toolPanel.add(clear);

		add(toolPanel, BorderLayout.NORTH); //宸ュ叿闈㈡澘鍒癆PPLET闈㈡澘

		setBounds(60, 60, 900, 600);
		setVisible(true);
		validate();

		//---------------------鏂囦欢鎵撳紑瀵硅瘽妗�-----------------------------------
		openPicture = new FileDialog(this, "鎵撳紑鍥剧敾", FileDialog.LOAD);
		openPicture.setVisible(false);
		savePicture = new FileDialog(this, "淇濆瓨鍥剧敾", FileDialog.SAVE);
		savePicture.setVisible(false);

		openPicture.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				openPicture.setVisible(false);
			}
		});

		savePicture.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				savePicture.setVisible(false);
			}
		});
		//---------------------鏂囦欢鎵撳紑瀵硅瘽妗�----------缁撴潫-------------------------

		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});

	}

	public void paint(Graphics g)
	{
		Graphics2D g2d = (Graphics2D) g;

		Point p1, p2;

		n = paintInfo.size(); //鐐逛俊鎭悜閲廣ec

		if (toolFlag == 2) //娓呴櫎鎸夐挳
		{
			g.clearRect(0, 0, getSize().width, getSize().height); // 娓呴櫎
		}

		for (int i = 0; i < n-1; i++)
		{
			p1 = (Point) paintInfo.elementAt(i);
			p2 = (Point) paintInfo.elementAt(i + 1);
			size = new BasicStroke(p1.boarder, BasicStroke.CAP_BUTT,
					BasicStroke.JOIN_BEVEL); //鐢荤瑪绮楃粏

			g2d.setColor(p1.col);
			g2d.setStroke(size);

			if (p1.tool == p2.tool)
			{
				switch (p1.tool)
				{
				case 0:// 鐢荤瑪

					Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
					g2d.draw(line1);
					break;

				case 1:// 姗＄毊
					g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
					break;

				case 3:// 鐢荤洿绾�
					Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
					g2d.draw(line2);
					break;

				case 4:// 鐢诲渾
					Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, Math
							.abs(p2.x - p1.x), Math.abs(p2.y - p1.y));
					g2d.draw(ellipse);
					break;

				case 5:// 鐢荤煩褰�
					Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, Math
							.abs(p2.x - p1.x), Math.abs(p2.y - p1.y));
					g2d.draw(rect);
					break;

				case 6:// 鎴柇锛岃烦杩�
					i = i + 1;
					break;

				default:
				}// end switch
			}// end if
		}// end for
	}

	public void itemStateChanged(ItemEvent e)
	{
		if (e.getSource() == ColChoice)// 棰勯�棰滆壊
		{
			String name = ColChoice.getSelectedItem();

			if (name == "black")
			{
				c = new Color(0, 0, 0);
			}
			else if (name == "red")
			{
				c = new Color(255, 0, 0);
			}
			else if (name == "green")
			{
				c = new Color(0, 255, 0);
			}
			else if (name == "blue")
			{
				c = new Color(0, 0, 255);
			}
		}
		else if (e.getSource() == SizeChoice)// 鐢荤瑪澶у皬
		{
			String selected = SizeChoice.getSelectedItem();

			if (selected == "1")
			{
				con = 1;
			}
			else if (selected == "3")
			{
				con = 3;
			}
			else if (selected == "5")
			{
				con = 5;
			}
			else if (selected == "7")
			{
				con = 7;
			}
			else if (selected == "9")
			{
				con = 9;
			}
			size = new BasicStroke(con, BasicStroke.CAP_BUTT,
					BasicStroke.JOIN_BEVEL); //璁剧疆鐢荤瑪澶у皬
		}
		else if (e.getSource() == EraserChoice) //璁剧疆姗＄毊澶у皬
		{
			String Esize = EraserChoice.getSelectedItem();

			if (Esize == "5")
			{
				Econ = 5 * 2;
			}
			else if (Esize == "9")
			{
				Econ = 9 * 2;
			}
			else if (Esize == "13")
			{
				Econ = 13 * 2;
			}
			else if (Esize == "17")
			{
				Econ = 17 * 3;
			}

		}

	}

	public void mouseDragged(MouseEvent e)
	{
		Point p1;
		switch (toolFlag)
		{
		case 0:// 鐢荤瑪
			x = (int) e.getX();
			y = (int) e.getY();
			p1 = new Point(x, y, c, toolFlag, con);
			paintInfo.addElement(p1);
			repaint();
			break;

		case 1:// 姗＄毊
			x = (int) e.getX();
			y = (int) e.getY();
			p1 = new Point(x, y, null, toolFlag, Econ);
			paintInfo.addElement(p1);
			repaint();
			break;

		default:
		}
	}

	public void mouseMoved(MouseEvent e)
	{
	}

	public void update(Graphics g)
	{
		paint(g);
	}

	public void mousePressed(MouseEvent e)
	{
		Point p2;
		switch (toolFlag)
		{
		case 3:// 鐩寸嚎
			x = (int) e.getX();
			y = (int) e.getY();
			p2 = new Point(x, y, c, toolFlag, con);
			paintInfo.addElement(p2);
			break;

		case 4: // 鍦�
			x = (int) e.getX();
			y = (int) e.getY();
			p2 = new Point(x, y, c, toolFlag, con);
			paintInfo.addElement(p2);
			break;

		case 5: // 鐭╁舰
			x = (int) e.getX();
			y = (int) e.getY();
			p2 = new Point(x, y, c, toolFlag, con);
			paintInfo.addElement(p2);
			break;

		default:
		}
	}

	public void mouseReleased(MouseEvent e)
	{
		Point p3;
		switch (toolFlag)
		{
		case 0:// 鐢荤瑪
			paintInfo.addElement(cutflag);
			break;

		case 1: // eraser
			paintInfo.addElement(cutflag);
			break;

		case 3:// 鐩寸嚎
			x = (int) e.getX();
			y = (int) e.getY();
			p3 = new Point(x, y, c, toolFlag, con);
			paintInfo.addElement(p3);
			paintInfo.addElement(cutflag);
			repaint();
			break;

		case 4: // 鍦�
			x = (int) e.getX();
			y = (int) e.getY();
			p3 = new Point(x, y, c, toolFlag, con);
			paintInfo.addElement(p3);
			paintInfo.addElement(cutflag);
			repaint();
			break;

		case 5: // 鐭╁舰
			x = (int) e.getX();
			y = (int) e.getY();
			p3 = new Point(x, y, c, toolFlag, con);
			paintInfo.addElement(p3);
			paintInfo.addElement(cutflag);
			repaint();
			break;

		default:
		}
	}

	public void mouseEntered(MouseEvent e)
	{
	}

	public void mouseExited(MouseEvent e)
	{
	}

	public void mouseClicked(MouseEvent e)
	{
	}

	public void actionPerformed(ActionEvent e)
	{

		if (e.getSource() == pen)// 鐢荤瑪
		{
			toolFlag = 0;
		}

		if (e.getSource() == eraser)// 姗＄毊
		{
			toolFlag = 1;
		}

		if (e.getSource() == clear)// 娓呴櫎
		{
			toolFlag = 2;
			paintInfo.removeAllElements();
			repaint();
		}

		if (e.getSource() == drLine)// 鐢荤嚎
		{
			toolFlag = 3;
		}

		if (e.getSource() == drCircle)// 鐢诲渾
		{
			toolFlag = 4;
		}

		if (e.getSource() == drRect)// 鐢荤煩褰�
		{
			toolFlag = 5;
		}

		if (e.getSource() == colchooser)// 璋冭壊鏉�
		{
//			Color newColor = JColorChooser.showDialog(this, "璋冭壊鏉�, c);
//			c = newColor;
		}

		if (e.getSource() == openPic)// 鎵撳紑鍥剧敾
		{
			openPicture.setVisible(true);

			if (openPicture.getFile() != null)
			{
				int tempflag;
				tempflag = toolFlag;
				toolFlag = 2;
				repaint();

				try
				{
					paintInfo.removeAllElements();
					File filein = new File(openPicture.getDirectory(),
							openPicture.getFile());
					picIn = new FileInputStream(filein);
					VIn = new ObjectInputStream(picIn);
					paintInfo = (Vector) VIn.readObject();
					VIn.close();
					repaint();
					toolFlag = tempflag;

				}

				catch (ClassNotFoundException IOe2)
				{
					repaint();
					toolFlag = tempflag;
					System.out.println("can not read object");
				}
				catch (IOException IOe)
				{
					repaint();
					toolFlag = tempflag;
					System.out.println("can not read file");
				}
			}

		}

		if (e.getSource() == savePic)//淇濆瓨鍥剧敾 
		{
			savePicture.setVisible(true);
			try
			{
				File fileout = new File(savePicture.getDirectory(), savePicture
						.getFile());
				picOut = new FileOutputStream(fileout);
				VOut = new ObjectOutputStream(picOut);
				VOut.writeObject(paintInfo);
				VOut.close();
			}
			catch (IOException IOe)
			{
				System.out.println("can not write object");
			}

		}
	}
}//end paintboard 